/*
 ============================================================================
 Name		: IconUtils.cpp
 Author	  : Den Grigorenko
 Copyright   : Copyright (c) 2008 Den123
 Description : IconUtils implementation
 ============================================================================
 */

#include <akniconutils.h> 
#include <aknutils.h> 
#include <gdi.h>
#include <e32math.h>

#include "IconUtils.h"

_LIT( KPathToMif, "\\resource\\apps\\CelestialSphere_0xEEC98BE4.mif" ); // file with icons

// create new icon from MIF-file
CGulIcon* IconUtils :: CreateIconL( TInt aIconId, TInt aIconMaskId )	
{
   CFbsBitmap* aBitmap; 
   CFbsBitmap* aMask;

   TFileName iMifFile( KPathToMif );

   AknIconUtils::CreateIconL( aBitmap, aMask, iMifFile, aIconId, aIconMaskId );
   CGulIcon* icon = CGulIcon::NewL( aBitmap, aMask );
   icon->SetBitmapsOwnedExternally( EFalse );
   
   return icon;
}

// set icon size
void IconUtils :: SetIconSizeL( CGulIcon* anIcon, TInt aWidth, TInt aHeight )	
{
   TSize iconSize( aWidth, aHeight );
   AknIconUtils :: SetSize( anIcon->Bitmap(), iconSize, EAspectRatioPreserved  ) ;    
   AknIconUtils :: SetSize( anIcon->Mask(), iconSize, EAspectRatioPreserved ) ;    
}

void IconUtils :: SetIconSizeL( CGulIcon* anIcon, TInt aWidth, TInt aHeight, TInt anAngle )	
{
   TSize iconSize( aWidth, aHeight );
   AknIconUtils :: SetSizeAndRotation( anIcon->Bitmap(), iconSize, EAspectRatioPreserved , anAngle ) ;    
   AknIconUtils :: SetSizeAndRotation( anIcon->Mask(), iconSize, EAspectRatioPreserved, anAngle ) ;    
}


// draw icon
void IconUtils :: DrawIconL( CBitmapContext& aGc, CGulIcon* anIcon, TInt aX, TInt aY )	
{	
    aGc.BitBltMasked( TPoint( aX, aY ), anIcon->Bitmap(), anIcon->Bitmap()->SizeInPixels(), anIcon->Mask(), EFalse );	
}

void IconUtils :: RotateBitmapL( CFbsBitmap* aBitmap, CFbsBitmap* aResultBitmap, TInt aRotateAngle )
{    
    TReal angle = ( KPi/180.0 )* (TReal)( 360 - aRotateAngle );
    TReal CosA = 0.0; Math::Cos(CosA, angle);
    TReal SinA = 0.0; Math::Sin(SinA, angle);
                   
    TBitmapUtil baseBmpUtil( aBitmap );       
    TBitmapUtil rotateBmpUtil( aResultBitmap );    
    
    baseBmpUtil.Begin(TPoint(0,0));    
    rotateBmpUtil.Begin(TPoint(0,0), baseBmpUtil);    
    rotateBmpUtil.Begin(TPoint(0,0));
        
    TSize size( aBitmap->SizeInPixels() );  
      
    TReal xC = size.iWidth/2,
          yC = size.iHeight/2;
    
    for(TInt new_y=0;new_y<size.iHeight;new_y++)
        for(TInt new_x=0;new_x<size.iWidth;new_x++)
        {
        	TReal deltaX = new_x - xC,
        		  deltaY = new_y - yC;
            if( deltaX * deltaX + deltaY * deltaY <= xC*yC )
			{
				TReal x = xC + ( deltaX * CosA + deltaY * SinA ); 
				TReal y = yC + (-deltaX * SinA + deltaY * CosA ); 
            
				baseBmpUtil.SetPos(TPoint(x,y));
				rotateBmpUtil.SetPos(TPoint(new_x,new_y));
				rotateBmpUtil.SetPixel(baseBmpUtil);
			}
        }
    
    baseBmpUtil.End();
    rotateBmpUtil.End();
}


